package com.ly.honor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.honor.entity.Honor;
import com.ly.honor.entity.dto.*;
import com.ly.honor.entity.vo.ClueHonorVO;
import com.ly.honor.entity.vo.HonorCountVO;
import com.ly.honor.entity.vo.HonorQueryVO;
import com.ly.honor.entity.vo.PlayerHonorQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.ClueMapper;
import com.ly.honor.mapper.HonorPlayerMapper;
import com.ly.honor.service.HonorService;
import com.ly.honor.mapper.HonorMapper;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 17798
* @description 针对表【bs_honor】的数据库操作Service实现
* @createDate 2022-11-16 11:49:49
*/
@Service
public class HonorServiceImpl extends ServiceImpl<HonorMapper, Honor>
    implements HonorService{

    @Autowired
    private HonorMapper honorMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private HonorPlayerMapper honorPlayerMapper;

    @Override
    public Page<HonorQueryVO> selectHonorPage(long pageNum,long pageSize,HonorQueryDTO honorQueryDTO) {
        var page = new Page<HonorQueryVO>(pageNum, pageSize);
        Page<HonorQueryVO> honorQueryVOPage = honorMapper.selectHonorPage(page,honorQueryDTO);
        return honorQueryVOPage;
    }

    @Override
    public HonorQueryVO getHonorById(long id) {
        var honor = honorMapper.selectById(id);
        if(ObjectUtils.isEmpty(honor)){
            throw new HonorException(ResultEnum.MESSAGE_INVALID);
        }
        var honorQueryVO = new HonorQueryVO();
        BeanUtils.copyProperties(honor,honorQueryVO);
        return honorQueryVO;
    }

    @Override
    public void insertHonor(HonorCreateDTO honorCreateDTO) {
        if(ObjectUtils.isEmpty(honorCreateDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        Integer clueId = honorCreateDTO.getClueId();
        if(clueId != null){
            var clue = clueMapper.selectById(clueId);
            if(ObjectUtils.isEmpty(clue)){
                throw new HonorException("找不到该俱乐部");
            }
        }
        var honor = new Honor();
        BeanUtils.copyProperties(honorCreateDTO,honor);
        honorMapper.insert(honor);
    }

    @Override
    public void updateHonor(HonorEditDTO honorEditDTO) {
        if(ObjectUtils.isEmpty(honorEditDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        Integer clueId = honorEditDTO.getClueId();
        if(clueId != null){
            var clue = clueMapper.selectById(clueId);
            if(ObjectUtils.isEmpty(clue)){
                throw new HonorException("找不到该俱乐部");
            }
        }
        var honor = new Honor();
        BeanUtils.copyProperties(honorEditDTO,honor);
        honorMapper.updateById(honor);
    }

    @Override
    public void deleteHonorById(long id) {
        honorPlayerMapper.deletePlayerByHonorId(id);
        honorMapper.deleteById(id);
    }

    @Override
    public void removeBatch(List<Long> idList) {
        honorPlayerMapper.removeBatchByHonor(idList);
        honorMapper.deleteBatchIds(idList);
    }

    @Override
    public Page<PlayerHonorQueryVO> getPlayerHonorPage(long pageNum, long pageSize, PlayerHonorQueryDTO playerDTO) {
        var page = new Page<PlayerHonorQueryVO>(pageNum, pageSize);
        Page<PlayerHonorQueryVO> playerHonorQueryVOPage = honorMapper.getPlayerHonorPage(page,playerDTO);
        List<PlayerHonorQueryVO> playerHonorQueryVOList = playerHonorQueryVOPage.getRecords();
        for (PlayerHonorQueryVO playerHonorQueryVO :playerHonorQueryVOList){
            var playerId = playerHonorQueryVO.getPlayerId();
            List<Integer> honorIdList = honorPlayerMapper.getHonorIdrByPlayer(playerId);
            List<String> honorList = honorPlayerMapper.getHonorByPlayer(playerId);
            playerHonorQueryVO.setHonorList(honorList);
            playerHonorQueryVO.setHonorIdList(honorIdList);
        }

        return playerHonorQueryVOPage;
    }

    @Override
    public List<Integer> getHonorByPlayer(Integer playerId) {
        List<Integer> honorIdList = honorPlayerMapper.getHonorIdrByPlayer(playerId);
        return honorIdList;
    }

    @Override
    public void updatePlayerHonor(PlayerHonorEditDTO playerHonorEditDTO) {
        //对playerHonorEditDTO中的荣誉id为空进行处理
        //删除该选手荣誉信息
        System.out.println(playerHonorEditDTO.getHonorIdList());
        var iterator = playerHonorEditDTO.getHonorIdList().listIterator();
        while(iterator.hasNext()){
            Integer honorId = iterator.next();
            if (ObjectUtils.isEmpty(honorId)) {
                //playerHonorEditDTO.getHonorIdList().remove(str);
                // 集合自身的remove()方法删除 会出现并发异常
                iterator.remove(); //迭代器的remove() 方法删除
            }
        }
        System.out.println(playerHonorEditDTO.getHonorIdList());
        honorPlayerMapper.deletePlayerByPlayerId(playerHonorEditDTO.getPlayerId());
        if(ObjectUtils.isEmpty(playerHonorEditDTO.getHonorIdList()) || playerHonorEditDTO.getHonorIdList().size() == 0){
            return;
        }
        //添加选手信息
        honorPlayerMapper.insertPlayerHonorList(playerHonorEditDTO);
    }

    @Override
    public void insertPlayerHonor(PlayerHonorCreateDTO playerHonorCreateDTO) {
        var honorIdList = honorPlayerMapper.getHonorIdrByPlayer(playerHonorCreateDTO.getPlayerId());
        honorIdList.forEach(honorId ->{
            if(honorId.equals(playerHonorCreateDTO.getHonorId())){
                throw new HonorException("该选手已有该荣誉");
            }
        });
        honorPlayerMapper.insertPlayerHonor(playerHonorCreateDTO);
    }

    @Override
    public Map<String,Object> findHonorCount() {
        List<HonorCountVO>  honorCountVOList = honorMapper.findHonorCount();
        var map = new HashMap<String, Object>();
        List<String> countryNameList = honorCountVOList.stream().map(HonorCountVO::getCountryName).collect(Collectors.toList());
        List<Integer> honorCountList = honorCountVOList.stream().map(HonorCountVO::getHonorCount).collect(Collectors.toList());
        map.put("xData",countryNameList);
        map.put("yData",honorCountList);
        return map;
    }

    @Override
    public List<JSONObject> getClueHonor() {
        List<ClueHonorVO> clueHonorVOList = honorMapper.getClueHonor();
        var clueHonorList = new ArrayList<JSONObject>();
        clueHonorVOList.forEach(clueHonorVO -> {
            var jsonObject = new JSONObject();
            jsonObject.put("value",clueHonorVO.getHonorCount());
            jsonObject.put("name",clueHonorVO.getClueName());
            clueHonorList.add(jsonObject);
        });
        return clueHonorList;
    }

    @Override
    public List<PlayerHonorQueryVO> getPlayerHonorAll() {
        List<PlayerHonorQueryVO> playerHonorQueryVOList = honorMapper.getPlayerHonorAll();
        for (PlayerHonorQueryVO playerHonorQueryVO :playerHonorQueryVOList){
            var playerId = playerHonorQueryVO.getPlayerId();
            List<Integer> honorIdList = honorPlayerMapper.getHonorIdrByPlayer(playerId);
            List<String> honorList = honorPlayerMapper.getHonorByPlayer(playerId);
            playerHonorQueryVO.setHonorList(honorList);
            playerHonorQueryVO.setHonorIdList(honorIdList);
        }

        return playerHonorQueryVOList;
    }
}




